package com.ai.ly.goodsautocat.common.wordfilter;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordFilterUtil {

    private static Pattern is2LengthLetterPattern = Pattern.compile("^[a-zA-Z]*");
    public static boolean is2LengthLetter(String word) {
        if (word.length()>2) {
            return false;
        }
        Matcher matcher = is2LengthLetterPattern.matcher(word);
        boolean result=matcher.matches();
        return result;
    }
    
    public static boolean isLetterAndNumber(String word) {
        boolean containsLetter=false;
        boolean containsNumber=false;
        for (int i = 0; i < word.length(); i++) {
            char c=word.charAt(i);
            
            if (Character.isLetter(c)) {
                containsLetter=true;
            }
            if (Character.isDigit(c)) {
                containsNumber=true;
            }
            
            if (containsLetter&&containsNumber) {
                return true;
            }
        }
        return false;
    }

    private static Pattern numberPattern = Pattern.compile("^[-\\+]?[.\\d]*$");
    public static boolean isNumber(String word) {
        Matcher matcher = numberPattern.matcher(word);
        boolean result=matcher.matches();
        return result;
    }
    
    static Map<Character, Object> puncMap=new HashMap<>();
    static {
        String puncs="`~!@#$^&*()=|{}':;',\\\\[\\\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？-+";
        Object object=new Object();
        for (int i = 0; i < puncs.length(); i++) {
            char c=puncs.charAt(i);
            puncMap.put(c, object);
        }
    }
    
    /**
     * 判断是否全是标点
     * @param word
     * @return
     * @author liangyi
     */
    public static boolean isAllPunc(String word) {
        if (word==null) {
            return false;
        }
        for (int i = 0; i < word.length(); i++) {
            char c=word.charAt(i);
            if (puncMap.get(c)==null) {
                return false;
            }
        }
        return true;
    }
}
